package com.mb;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;

import com.common.util.messages.MessagesController;
import com.facade.MemberFacade;
import com.model.Member;
import com.model.Membership;
import com.model.User;
import com.objects.MemberSearch;

@ViewScoped
@ManagedBean
public class MemberMB extends AbstractMB implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private Member member;
	private int membershipId;
	private Membership membership;
	private List<Member> members;
	private List<Member> activeMembers;
	private List<Member> filteredMembers;
	private Member selectedMember;
	private MemberSearch memberSearch;
	
	private MemberFacade memberFacade; 
	
	MessagesController msgCtrl = new MessagesController();
	
	public void createMember() {
		try {
			
			UserMB userMB = new UserMB();
			User user = userMB.getLoggedUser();
			
			member.setCreatedDate(new Date());
			member.setCreatedBy(user);
			member.setUpdatedDate(new Date());
			member.setUpdatedBy(user);
			member.setMembershipLastPaymentDate(member.getMembershipExpiryDate());
			member.setActive(true);
			member.setSuspend(false);
			
			getMemberFacade().createMember(member);
			closeDialog();
			msgCtrl.addInfo("New Member Registered", member.getName() + " is saved successfully");
			member = new Member();
		} catch (Exception e) {
			keepDialogOpen();
			msgCtrl.addFatal("Uhh Ohh", "something is not right here.");
			e.printStackTrace();
		}
	}
	
	public void updateMember() {
		try {
			
			UserMB userMB = new UserMB();
			User user = userMB.getLoggedUser();
			
			member.setUpdatedDate(new Date());
			member.setUpdatedBy(user);
			
			getMemberFacade().updateMember(member);
			closeDialog();
			msgCtrl.addInfo("Member Info Updated", member.getName() + " is updated successfully");
			loadMembers();
			resetMember();
		} catch (Exception e) {
			keepDialogOpen();
			msgCtrl.addFatal("Uhh Ohh", "something is not right here.");
			e.printStackTrace();
		}
	}	
	
	public void suspendTerminateMember() {
		try {
			
			UserMB userMB = new UserMB();
			User user = userMB.getLoggedUser();
			
			member.setUpdatedBy(user);
			member.setUpdatedDate(new Date());
			
			getMemberFacade().suspendTerminateMember(member);
			closeDialog();
			msgCtrl.addInfo("Member is Suspended/Terminated", member.getName() + " is updated successfully");
			loadMembers();
			resetMember();
		} catch (Exception e) {
			keepDialogOpen();
			msgCtrl.addFatal("Uhh Ohh", "something is not right here.");
			e.printStackTrace();
		}
	}
	
	public void packageExpiryMember() {
		try {
			
			UserMB userMB = new UserMB();
			User user = userMB.getLoggedUser();
			
			member.setUpdatedBy(user);
			member.setUpdatedDate(new Date());
			
			getMemberFacade().changeExpiryMember(member);
			closeDialog();
			msgCtrl.addInfo("Membership package/expiry date is change", member.getName() + " is updated successfully");
			loadMembers();
			resetMember();
		} catch (Exception e) {
			keepDialogOpen();
			msgCtrl.addFatal("Uhh Ohh", "something is not right here.");
			e.printStackTrace();
		}
	}	
	
	public void search(){
		members = getMemberFacade().search(memberSearch);
	}
	
	public MemberFacade getMemberFacade() {
		if (memberFacade == null) {
			memberFacade = new MemberFacade();
		}		
		return memberFacade;
	}
	
	public void setMemberFacade(MemberFacade memberFacade) {
		this.memberFacade = memberFacade;
	}
	
	public Member getMember() {
		if (member == null) {
			member = new Member();
		}
		return member;
	}
	
	public void setMember(Member member) {
		this.member = member;
	}
	
	public List<Member> getAllMembers() {
		if (members == null) {
			loadMembers();
		}

		return members;
	}	
	
	private void loadMembers() {
		members = getMemberFacade().listAll();
	}
	
	public void resetMember() {
		member = new Member();
	}
	
	private void loadActiveMembers(){
		activeMembers = getMemberFacade().listActiveMembers();
	}
	
	public void preSelectedMember(Member selectedMember){
		FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().put("validatorMemberUpdate", selectedMember);
	}
	
	public List<Member> getFilteredMembers() {
		return filteredMembers;
	}

	public void setFilteredMembers(List<Member> filteredMembers) {
		this.filteredMembers = filteredMembers;
	}	
	
    public void onRowSelect(SelectEvent event) { 

    }  
    
    public void onRowUnselect(UnselectEvent event) {  

    }

	public Member getSelectedMember() {
		return selectedMember;
	}

	public void setSelectedMember(Member selectedMember) {
		this.selectedMember = selectedMember;
	}

	public List<Member> getMembers() {
		return members;
	}

	public void setMembers(List<Member> members) {
		this.members = members;
	}

	public int getMembershipId() {
		return membershipId;
	}

	public void setMembershipId(int membershipId) {
		this.membershipId = membershipId;
	}

	public List<Member> getActiveMembers() {
		if(activeMembers == null){
			loadActiveMembers();
		}
		return activeMembers;
	}

	public void setActiveMembers(List<Member> activeMembers) {
		this.activeMembers = activeMembers;
	}

	public Membership getMembership() {
		return membership;
	}

	public void setMembership(Membership membership) {
		this.membership = membership;
	}

	public MemberSearch getMemberSearch() {
		if(memberSearch == null){
			memberSearch = new MemberSearch();
		}
		return memberSearch;
	}

	public void setMemberSearch(MemberSearch memberSearch) {
		this.memberSearch = memberSearch;
	}	
	
	
    
	
}
